package db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBLocationManager {
	static Connection con = null;
	static PreparedStatement lookupLocation, addLocation;
	
	public static final int ALREADY_EXISTS=-2;
	private static void getConnection() throws SQLException{
		if (!Connector.isValidConnection(con)) {
			con = Connector.getConnection();
			//prepare statements
			lookupLocation = con.prepareStatement("Select * from tblLocation where locationName=?");
		    addLocation=con.prepareStatement("insert into tblLocation (locationName) values (?)");
		   
		}
	}
	
	public  static int getLocationID(String loc){
		try {
			getConnection();
			
			lookupLocation.clearParameters();
			lookupLocation.setString(1, loc);
			
			ResultSet rs=lookupLocation.executeQuery();
			while(rs.next()){
				 return rs.getInt("locationID");
			}
			return -1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
	}

	public  static boolean addLocation(String loc){
		try {
			getConnection();
			
			addLocation.clearParameters();
			addLocation.setString(1, loc);
			
			int rs=addLocation.executeUpdate();
			
			return (rs==1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	

	
		
}
